package org.MyToolsProject;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

public class YueYuMain {

    public static void main3(String[] args) {
        String dir = "C:\\Users\\taher\\Desktop\\DFT-粤语\\";
        Map<Character, String> characterMap = getCharacterMap();
        StringBuilder sb = new StringBuilder();
        HashSet<Character> exists = new HashSet<>();
        try {
            List<String> lines = FileUtils.readLines(new File(dir + "解说词-繁体.txt"), "UTF-8");
            for (String line : lines) {
                char[] chars = line.toCharArray();
                for (char c : chars){
                    String py = characterMap.get(c);
                    if( py == null ){
                        exists.add(c);
                    }
                }
            }
            for (Character c : exists){
                sb.append(c).append(",");
            }
            System.out.println(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String dir = "C:\\Users\\taher\\Desktop\\DFT-粤语\\";
        Map<Character, String> characterMap = getCharacterMap();
        StringBuilder sb = new StringBuilder();

        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(dir + "解说词-繁体-注音版.txt", true));
            List<String> lines = FileUtils.readLines(new File(dir + "解说词-繁体.txt"), "UTF-8");
            for (String line : lines) {
                sb.setLength(0);
                char[] chars = line.toCharArray();
                for (char c : chars){
                    String py = characterMap.get(c);
                    if( py != null ){
                        sb.append(c).append("<").append(py).append(">");
                    }else {
                        sb.append(c);
                    }
                }
                IOUtils.write(sb + System.lineSeparator(), writer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Map<Character,String> getCharacterMap() {
        String dir = "C:\\Users\\taher\\Desktop\\DFT-粤语\\";
        Map<Character,String> datas = new HashMap<>(7000);
        try {
            List<String> lines = FileUtils.readLines(new File(dir + "7000字粤语注音.txt"), "UTF-8");
            for (String line : lines) {
                int i = line.indexOf(" ");
                char k = line.substring(0,i).charAt(0);
                String v = line.substring(i+1).trim();
                datas.put(k,v);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return datas;
    }

    public static void main2(String[] args) {
        String dir = "F:\\粤语资料\\文档\\詞頻7000\\";
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(dir + "7000字粤语注音.txt", true));
            List<String> lines = FileUtils.readLines(new File(dir + "test.txt"), "UTF-8");
            for (String line : lines) {
                int i = line.indexOf(" ");
                line = line.substring(i+1).trim();
                IOUtils.write(line + System.lineSeparator(), writer);
                /*i = line.indexOf(" ");
                String k = line.substring(0,i);
                String v = line.substring(i+1);
                System.out.println(k + "-" + v);
                */
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
